package milk.scene
{
	import com.crlui.component.TabNavigate;
	import com.crlui.control.TabItem;
	import com.crlui.core.IResizeable;
	import com.crlui.core.state.IState;
	import com.crlui.manager.ResizeManager;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import milk.scene.part.IntroduceSprite;
	import milk.ui.TabItemSkin;
	
	import ui.MainScene;
	
	public class SceneApp extends MainScene implements IState,IResizeable
	{
		public static const NAME:String="SceneApp";
		
		
		protected var _initialized:Boolean;
		protected var maxH:int=500;
		private var padX:int;
		private var padY:int;
		
		
		public function SceneApp()
		{
			this.name=NAME;
		}

		
		public function get type():String
		{
			return name;
		}
		
		public function get initialized():Boolean
		{
			return _initialized;
		}
		
		public function initialize():void
		{
			_initialized=true;
			
			padX=15;
			padY=15;
			
			loading.visible=false;
			
			var labels:Array="公司,产品介绍,联系方式".split(",");
			var targets:Array=[new IntroduceSprite()];
			var len:int=labels.length;
			
			
			while(this.content.numChildren){
				this.content.removeChildAt(0);
			}
			var tabNav:TabNavigate=new TabNavigate(this.content);
			tabNav.addEventListener(Event.CHANGE,tabChangeHandle);
			var tabItem:TabItem;
			for(var i:int=0;i<len;i++){
				tabItem=new TabItem(new TabItemSkin(this["tabItem"+i]));
				tabItem.label=labels[i];
				tabItem.target=targets[i];
				tabNav.add(tabItem);
			}
			tabNav.selectedIndex=0;
		}
		
		public function start():void
		{
			ResizeManager.instance.add(this);
		}
		
		public function end():void
		{
			ResizeManager.instance.remove(this);
		}
		
		private function tabChangeHandle(event:Event):void{
			var tabItem:TabItem=(event.target as TabNavigate).selectedItem;
			var target:DisplayObject=tabItem.target;
			
			if(target==null)return;
			
			background.width=target.width+2*padX;
			background.height=target.height+2*padY;
			
			ResizeManager.instance.update(this);
		}
		
		public function resize(w:int, h:int):void
		{
			this.loading.x=(background.width-loading.width)>>1;
			this.loading.y=(background.height-loading.height)>>1;
			this.x=(w-background.width)>>1;
			this.y=(h-background.height)>>1;
		}
		
	}
}